package br.com.arosa.dao;

import java.util.*;
import java.sql.*;
import br.com.easynet.database.DataSet;
import br.com.jdragon.dao.*;
import br.com.arosa.transfer.*;

public class Tb_gradeDAO extends ObjectDAO { 
	 public Tb_gradeDAO(DAOFactory dao) throws Exception {
		 setDAOFactory(dao);
 		 con = dao.create();
 	 }

	 public void insert(Tb_gradeT tb_gradeT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "insert into tb_grade  ( id_grade, id_produto, tamanho) values ( ? , ? , ? )";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_gradeT.getId_grade());
			 pStmt.setObject(2, tb_gradeT.getId_produto());
			 pStmt.setObject(3, tb_gradeT.getTamanho());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void update(Tb_gradeT tb_gradeT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "update tb_grade set  id_produto=?, tamanho=?  where  id_grade=?";
 			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_gradeT.getId_produto());
			 pStmt.setObject(2, tb_gradeT.getTamanho());
			 pStmt.setObject(3, tb_gradeT.getId_grade());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void delete(Tb_gradeT tb_gradeT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "delete from tb_grade where  id_grade=?";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_gradeT.getId_grade());
			 pStmt.execute();
		 } catch (Exception e)  {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 private List<Tb_gradeT> resultSetToObjectTransfer(ResultSet rs) throws Exception { 
		 List<Tb_gradeT> objs = new Vector();
		 while (rs.next()) { 
 			 Tb_gradeT tb_gradeT = new Tb_gradeT();
			 tb_gradeT.setId_grade(rs.getInt("id_grade"));
			 tb_gradeT.setId_produto(rs.getInt("id_produto"));
			 tb_gradeT.setTamanho(rs.getString("tamanho"));
			 objs.add(tb_gradeT);
 		 }
 		 return objs; 
 	 }
	 public List<Tb_gradeT> getAll() throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_grade"; 
			 pStmt = con.prepareStatement(sql);
			 rs = pStmt.executeQuery();
 			 List<Tb_gradeT> list = resultSetToObjectTransfer(rs);
 			 return list; 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_gradeT> getById(Tb_gradeT tb_gradeT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_grade where  id_grade=?"; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_gradeT.getId_grade());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_gradeT> getById_grade(Tb_gradeT tb_gradeT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_grade where  id_grade = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_gradeT.getId_grade());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_gradeT> getById_produto(Tb_gradeT tb_gradeT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_grade where  id_produto = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_gradeT.getId_produto());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_gradeT> getByTamanho(Tb_gradeT tb_gradeT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_grade where  Upper(tamanho) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_gradeT.getTamanho()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 

 }